package flexulous.util;

import flex2.tools.oem.Application;
import flex2.tools.oem.Configuration;
import org.gjt.sp.util.Log;
import java.io.*;
import java.util.Observable;
import flex2.tools.oem.ProgressMeter;
import org.gjt.sp.util.Log;
import errorlist.*;

import flexulous.FlexulousPlugin;
import flexulous.model.FlexulousTarget;

public class FlexulousCompileThread extends Observable implements Runnable, ProgressMeter
{
	
	private Application application;
	private long before, after;
	
	private int percentComplete;
	public int getPercentComplete()
	{
		return percentComplete;
	}
	
	private FlexulousTarget target;
	
	public long getCompileTime()
	{
		return after - before;
	}
	
	public FlexulousCompileThread(Application application, FlexulousTarget target)
	{
		this.application = application;
		this.target = target;
	}
	
	public void run()
	{
		try
		{
			String growlLocation = "";
			boolean useGrowl = FlexulousPlugin.getModel().getGrowlIsValid();
			
			if(useGrowl)
			{
				growlLocation = FlexulousPlugin.getModel().getGrowlLocation();
			}
			
			application.setProgressMeter(this); 
			
			if(application.getLogger() == null)
			{
				application.setLogger(new FlexulousLogger());
			}
			
			Log.log(Log.WARNING, FlexulousCompileThread.class, "About to compile.");
			
			String os = System.getProperty("os.name").toLowerCase();
			
			if (application.build(true) == 0)
			{
				Log.log(Log.WARNING, FlexulousCompileThread.class, "Build Failed");
				FlexulousPlugin.getErrorSource().addError(new DefaultErrorSource.DefaultError(FlexulousPlugin.getErrorSource(),ErrorSource.ERROR,target.getSource(),0,0,0,"[FAILED: "+target.getName()+" ]"));
				
				if(useGrowl)
				{
					if(os.indexOf("win") >= 0)
					{
						Runtime.getRuntime().exec(new String[] { growlLocation, "/t:jEdit", target.getName() + ": FAILED."});
					}
					else if(os.indexOf("mac") >= 0)
					{
						Runtime.getRuntime().exec(new String[]{growlLocation,"-a", "jEdit", "-m",target.getName()+": FAILED."});
					}
				}
				
				setChanged();
				notifyObservers(FlexulousCompilerHelper.BUILD_FAILED);
			}
			else
			{
				FlexulousPlugin.getErrorSource().addError(new DefaultErrorSource.DefaultError(FlexulousPlugin.getErrorSource(),ErrorSource.WARNING,"",0,0,0,"[SUCCESS: "+target.getName()+" ]"));
				Log.log(Log.WARNING, FlexulousCompileThread.class, "Compiled awesomely.");
				
				if(useGrowl)
				{
					if(os.indexOf("win") >= 0)
					{
						Runtime.getRuntime().exec(new String[] { growlLocation, "/t:jEdit", target.getName() + ": SUCCESS."});
					}
					else if(os.indexOf("mac") >= 0)
					{
						Runtime.getRuntime().exec(new String[]{growlLocation,"-a", "jEdit", "-m",target.getName()+": SUCCESS."});
					}
				}
				
				setChanged();
				
				notifyObservers(FlexulousCompilerHelper.COMPILED);
			}
            
			Log.log(Log.WARNING, FlexulousCompileThread.class, "Finished compiling");	
		}
		catch(FileNotFoundException fnf)
		{
			setChanged();
			notifyObservers(FlexulousCompilerHelper.OUTPUT_FILE_NOT_FOUND);
		}
		catch(IOException ex)
		{
			Log.log(Log.WARNING, FlexulousCompileThread.class, "WTF! "+ex);
		}
		catch(Exception ex) 
		{
			Log.log(Log.ERROR, FlexulousCompileThread.class, "Exception: " + ex);
		}
	}
	
	public void start()
	{ 
		before = System.currentTimeMillis();
		setChanged();
		notifyObservers(FlexulousCompilerHelper.COMPILE_PROGRESS_START);
	}
	
	public void end()
	{ 
		after = System.currentTimeMillis();
		setChanged();
		notifyObservers(FlexulousCompilerHelper.COMPILE_PROGRESS_END);
		//System.out.println("done"); 
		//System.out.println("Elapsed Time: " + (after - before) + "ms");
	}
	
	public void percentDone(int n)
	{ 
		//System.out.print(n + "...");
		percentComplete = n;
		setChanged();
		notifyObservers(FlexulousCompilerHelper.COMPILE_PROGRESS);	
	}
}
